﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TfsBugger.Core;

namespace TfsBugger.Controls
{
    /// <summary>
    /// Interaction logic for BuggerStackItem.xaml
    /// </summary>
    public partial class BuggerStackItemControl : UserControl
    {
        public BuggerStackItemControl(Bugger bugger)
        {
            InitializeComponent();
            m_bugger = bugger;
            m_bugger.BeginWork += OnBuggerBeginWork;
            m_bugger.ArgsLoaded += OnBuggerArgsLoaded;
            m_bugger.PropertyChanged += OnBuggerPropertyChanged;
            m_bugger.PollingStatusUpdated += OnPollingStatusUpdated;
            m_bugger.TargetUpdated += OnTargetStatusUpdated;
            m_bugger.WorkFinished += OnWorkFinished;
        }


        private Bugger m_bugger;


        private Storyboard DotTransformStoryboard
        {
            get { return (Storyboard)Resources["DotTransformStoryboard"]; }
        }


        public Bugger Bugger
        {
            get { return m_bugger; }
        }


        #region UI Event Handlers
        private void OnControlLoaded(object sender, RoutedEventArgs e)
        {
            this.lblName.Text = Bugger.Name;
            this.lblRunningStatus.Text = Bugger.IsBusy ? "Running" : "Ready";
            this.lblCurStatus.Text = "Waiting";

            //this.lnkStopStart.IsEnabled = Bugger.CheckArgsLoaded();
        }
        #endregion


        #region Bugger Event Handlers
        private void OnBuggerBeginWork(object sender, EventArgs e)
        {
            this.lblRunningStatus.Text = "Running";
            AnimCanvasContainer.Visibility = Visibility.Visible;
            DotTransformStoryboard.Begin(this, true);
        }


        private void OnBuggerPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName.ToLower() == "name")
                this.lblName.Text = Bugger.Name;
        }


        private void OnBuggerArgsLoaded(object sender, EventArgs e)
        {
            this.lblServerName.Text = Bugger.Args.ServerManager.ToString();
        }


        private void OnPollingStatusUpdated(object sender, PollingStatusUpdatedEventArgs e)
        {
            this.lblCurStatus.Text = e.Status;
        }


        private void OnTargetStatusUpdated(object sender, TargetUpdatedEventArgs e)
        {

        }


        private void OnWorkFinished(object sender, BuggerWorkFinishedEventArgs e)
        {
            this.lblRunningStatus.Text = "Ready";
            DotTransformStoryboard.Stop(this);
            AnimCanvasContainer.Visibility = Visibility.Hidden;
        } 
        #endregion
    }
}
